using System;
using cfg.level;
using UnityEngine;
using QFramework;
using UnityEngine.Serialization;

namespace QFramework.Monk
{
	public partial class SimpleCircle : AbilityBase
	{
		private float mAngle; // 初始化角度
		private Quaternion mLastQ = Quaternion.identity; // 上一次的四元数
		public override void InitAbility()
		{
			//CircleA.Show();
			mSpeed = 2;
			mCount = 1;
			//mLevel = 0;
			//this.Show();
			//this.Hide();
			
			// Code Here
			CircleA.OnTriggerEnter2DEvent(collider2D =>
			{
				if (collider2D.gameObject.layer == LayerMask.NameToLayer("Enemy"))
				{
					var enemy = collider2D.GetComponentInParent<EnemyBase>();
					if (enemy!=null)
					{
						//是否可以击退敌人
						var dir = (enemy.Position()-this.Position()).normalized;
						enemy.HitBack(dir*mHitBackFactor);
						enemy.CostHp(AppStart.damage.Value);
					}
				}
				
			}).UnRegisterWhenGameObjectDestroyed(CircleA.gameObject);
			CircleA.Show();
			
			CircleB.OnTriggerEnter2DEvent(collider2D =>
			{
				if (collider2D.gameObject.layer == LayerMask.NameToLayer("Enemy"))
				{
					var enemy = collider2D.GetComponentInParent<EnemyBase>();
					if (enemy!=null)
					{
						//是否可以击退敌人
						var dir = (enemy.Position()-this.Position()).normalized;
						enemy.HitBack(dir*mHitBackFactor);
						enemy.CostHp(AppStart.damage.Value);
					}
				}
				
			}).UnRegisterWhenGameObjectDestroyed(CircleB.gameObject);
			CircleB.Hide();
			
			//base.ShowAbility();
		}

		protected override void OnCountAdd()
		{
			CircleB.Show();
		}


		//Circle围绕玩家以radius为半径顺时针旋转
		private void Update()
		{
			if (!AppStart.CheckGameState(GameState.GamePlaying))
			{
				return;
			}
			// 更新角度
			mAngle +=  Time.deltaTime*mSpeed*60;
			//转四元数
			var  newQ = Quaternion.AngleAxis(mAngle, Vector3.forward);
			// 使用插值平滑位置变化
			var quat = Quaternion.Lerp(mLastQ, newQ, Time.deltaTime*mSpeed);
			// 更新上一次的位置
			mLastQ = newQ;
			this.transform.localRotation = quat;
			
			//如果是超级技能还会内外伸缩
			if (mIsSuper)
			{
				float xPosition = 2 +  Mathf.Cos(mAngle.Deg2Rad());
				CircleA.LocalPositionX(xPosition);
				CircleB.LocalPositionX(-xPosition);
			}
		}
	}
}
